package com.esotericsoftware.reflectasm.benchmark;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.TreeMap;

public class Benchmark {
    static int testRounds = 300000;
    static int testCount = (System.getProperty("java.vm.name").indexOf("Server VM") > -1) ? 300 : 100;
    public boolean warmup = true;
    public Map<String, Long> testTimes = new TreeMap<>();
    private long s;

    public void start() {
        if (warmup) return;
        s = System.nanoTime();
    }

    public void end(String name) {
        if (warmup) return;
        long e = System.nanoTime();
        long time = e - s;
        /*
        if(System.getProperty("java.vm.name").indexOf("Server VM")>-1) name="ServerVM["+name+"]";
        else name="ClientVM["+name+"]";
        */
        Long oldTime = testTimes.get(name);
        if (oldTime == null || time < oldTime) testTimes.put(name, time);
        System.out.println(name + ": " + time / 1000000f + " ms");
    }

    public String[] chart(String title) {
        Comparator<Entry> comparator = new Comparator<Entry>() {
            public int compare(Entry o1, Entry o2) {
                // return ((String)o1.getKey()).compareTo((String)o2.getKey());
                return (int) ((Long) o1.getValue() - (Long) o2.getValue());
            }
        };
        ArrayList<Entry> list = new ArrayList(testTimes.entrySet());
        //Collections.sort(list, comparator);

        StringBuilder names = new StringBuilder(512);
        StringBuilder times = new StringBuilder(512);
        long max = 0;
        int count = 0;

        for (Entry<String, Long> entry : list) {
            String name = entry.getKey();
            names.insert(0, '|');
            names.insert(0, name);
            long time = entry.getValue();
            times.append(time);
            times.append(',');
            max = Math.max(max, time);
            count++;
        }
        times.setLength(times.length() - 1);
        names.setLength(names.length() - 1);
        int height = count * 18 + 21;
        int width = Math.min(700, 300000 / height);
        System.out.println("![](http://chart.apis.google.com/chart?chtt=" + title + "&" + "chs=" + width + "x" + height + "&chd=t:" + times + "&chds=0," + max + "&chxl=0:|" + names + "&cht=bhg&chbh=10&chxt=y&" + "chco=660000|660033|660066|660099|6600CC|6600FF|663300|663333|" + "663366|663399|6633CC|6633FF|666600|666633|666666)\n");
        return new String[]{times.toString(), String.valueOf(max), names.toString()};
    }
}
